package cn.tedu.task;





import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * 海洋物体类，是所有类的父类
 */
public abstract class SeaObject {

    /**
     * The constant LIVE.
     */
    public static final int LIVE = 0;//0目标存活状态

    /**
     * The constant DEAD.
     */
    public static final int DEAD = 1; //1目标死状态
    private int state = LIVE; //默认状态：活着

    /**
     * The Width.
     */
    protected int width;//海洋目标宽属性
    /**
     * The Height.
     */
    protected int height;//海洋目标高属性
    /**
     * The X.
     */
    protected int x;//x坐标
    /**
     * The Y.
     */
    protected int y;//y坐标
    /**
     * The Speed.
     */
    protected int speed;//移速属性


    /**
     * Instantiates a new Sea object.
     *
     * @param width  the width
     * @param height the height
     */
    public SeaObject(int width, int height) {
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT - height - 150 + 1) + 150;
        speed = rand.nextInt(3) + 1;
    }


    /**
     * Instantiates a new Sea object.
     *
     * @param width  the width
     * @param height the height
     * @param x      the x
     * @param y      the y
     * @param speed  the speed
     */
    public SeaObject(int width, int height, int x, int y, int speed) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }


    /**
     * Move.
     */
    public abstract void move();

    /**
     * Gets image.
     *
     * @return the image
     */
    public abstract ImageIcon getImage();


    /**
     * Is live boolean.
     *
     * @return the boolean
     */
    public boolean isLive() {
        return state == LIVE;
    }


    /**
     * Is dead boolean.
     *
     * @return the boolean
     */
    public boolean isDead() {
        return state == DEAD;
    }

    /**
     * 检测潜艇越界 @return the boolean
     */
    public boolean isOutOfBounds(){
        return x>=World.WIDTH;
    }

    /**
     * 检测碰撞 @param other the other
     *
     * @return the boolean
     */
    public boolean isHit(SeaObject other){
        int x1=this.x-other.width;
        int x2=this.x+this.width;
        int y1=this.y-other.height;
        int y2=this.y+this.height;
        int x=other.x;
        int y=other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    /**
     * 海洋对象状态修改
     */
    public void goDead(){
        state=DEAD;
    }


    /**
     * Paint image.
     *
     * @param g the g
     */
    public void paintImage(Graphics g) {
        if (this.isLive()) {
            this.getImage().paintIcon(null, g, this.x, this.y);
        }

    }
}









